# import requests
# import unittest,ddt
# -*- coding:utf-8 -*-
# url = 'http://127.0.0.1:8000/hello/'
#
# @ddt.ddt
# class TestHello(unittest.TestCase):
#     @ddt.data(*[({'name':'das'},200,'das'),
#                 ('',200,'没有的人')])
#     @ddt.unpack
#     def test_hello(self,data,status_code,msg):
#         aa = requests.get(url,params=data)
#         # print(aa)
#         self.assertEqual(aa.status_code,status_code)
#         self.assertIn(msg,aa.text)
#
# if __name__ == '__main__':
#     unittest.main()

#引入
import unittest,ddt

from cs_zhexing import jiekou_xlsx
from hanshu_requests import OperaData,http_requests

df = OperaData(r'D:\pythonProject1\Jiekou\qwer\cs.xlsx')


# url = 'http://127.0.0.1:8000/hello/'
@ddt.ddt
class HelloTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:

        cls.result_list = []

    # 返回的是个列表，加*解包
    @ddt.data(*df.get_case_data())
    # @ddt.data(*jiekou_xlsx(r'D:\pythonProject1\Jiekou\qwer\cs.xlsx','hello'))
    @ddt.unpack
                            #根据测试用例取需要参数
    def test_hello(self,url,method,keys,data,status_code,msg):
        # res = requests.get(url,params=data)
        # print(url)
        # print(method)
        # print(method(type))
        # print(keys)
        # print(data)
        # print(status_code)
        # print(msg)
        if data:
            print(data)
            data = eval(data)
        try:
            # 发起请求返回一个响应对象res
            res = http_requests(url,method,keys,data)
            # 判断状态码是否相等
            self.assertEqual(res.status_code,status_code)
            # 判断msg是否在res.text里
            self.assertIn(msg,res.text)
            result = 'yes'
        except:
            result = 'no'
            # 抛出错误
            raise
        finally:
            # 执行结果都添加到result_list列表里
            self.result_list.append(result)

    @classmethod
    def tearDownClass(cls) -> None:
        # 调用保存函数，把结果保存到‘是否通过’列里，
        df.save_data('是否通过', cls.result_list)


if __name__ == '__main__':
    unittest.main()

